﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ModelLibrary;
using System.Text;

namespace WebC
{
    public partial class RepairApp : clsBaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(RepairApp));
            if (!IsPostBack)
            {
                lbAppDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                loadInfo();
            }
        }

        public string sFileName = "appfile";

        #region 载入信息

        public void loadInfo()
        {
            BuyDevice buy = null;
            DeviceInfo deviceinfo = new DeviceInfo();
            string sModelName = "";
            DataSet ds = null;
            //divAudi.Visible = false;
            try
            {
                buy = new BuyDevice();
                if (m_Id == 0)
                {
                    if (!checkModelRightById(153, ref sModelName, (clsBaseLib)buy))
                    {
                        errorUrl(string.Format("你没有“{0}”的权限,请与管理员联系！", sModelName));
                        return;
                    }
                    DataTable dt = buy.getUserDept(iUserId);
                    //txtDeptId.Text = Convert.ToString(dt.Rows[0]["deptid"]);
                    //txtDeptName.Text = Convert.ToString(dt.Rows[0]["deptname"]);                    
                }
                else
                {
                    if (Convert.ToInt32(Request["audi"]) == 1)
                    {
                        if (!checkModelRightById(156, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                        trbtn.Visible = false;
                        //divAudi.Visible = true;
                    }
                    else if (Convert.ToInt32(Request["noedit"]) == 1)
                    {
                        if (!checkModelRightById(152, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    
                    else
                    {
                        if (!checkModelRightById(155, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    //ds = buy.getAppInfoById(m_Id);
                    ds = deviceinfo.getRepairPlanAppInfoById(m_Id);
                }
                sModelFullName = sModelName;

            }
            catch (Exception e)
            {
                errorUrl(Convert.ToString(e));
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            if (m_Id > 0)
            {
                if (ds != null)
                {
                    DataTable dt = ds.Tables[0];
                    if (dt.Rows.Count == 1)
                    {
                        Button_Dev.Visible = false;
                        txtAppCode.Enabled = false;
                        txtDevID.Value = Convert.ToString(dt.Rows[0]["DevID"]);
                        txtAppCode.Text = Convert.ToString(dt.Rows[0]["PlanCode"]);
                        lbAppDate.Text = Convert.ToDateTime(dt.Rows[0]["dCreateTime"]).ToString("yyyy-MM-dd");
                        lbDevCode.Text = Convert.ToString(dt.Rows[0]["DeviceCode"]);
                        lbDevName.Text = Convert.ToString(dt.Rows[0]["DeviceName"]);
                        lbUesDept.Text = Convert.ToString(dt.Rows[0]["DeptName"]);
                        lbDevSpec.Text = Convert.ToString(dt.Rows[0]["Devicespec"]);
                        if (dt.Rows[0]["dPlanBeginTime"] != null && dt.Rows[0]["dPlanBeginTime"].ToString().Trim() != "")
                        {
                            txtBeginDate.Text = Convert.ToDateTime(dt.Rows[0]["dPlanBeginTime"]).ToString("yyyy-MM-dd");
                        }
                        if (dt.Rows[0]["dPlanEndTime"] != null && dt.Rows[0]["dPlanEndTime"].ToString().Trim() != "")
                        {
                            txtEndDate.Text = Convert.ToDateTime(dt.Rows[0]["dPlanEndTime"]).ToString("yyyy-MM-dd");
                        }
                        txtNeed.Text = Convert.ToString(dt.Rows[0]["RepairNeed"]);

                    }
                    #region //获取维修计划维修项目列表
                    dt = ds.Tables[1];
                    int iRowCount = dt.Rows.Count;
                    StringBuilder sbu = new StringBuilder();
                    for (int i = 0; i < iRowCount; i++)
                    {
                        sbu.Append(string.Format(@"<tr>
                        <td align=""left"" width=""120"">
                            <a href=""javascript:"" onclick=""insertdevice(this.parentNode.parentNode)"">+</a> <a href=""javascript:"" onclick=""removeItem(this.parentNode.parentNode)"">-</a><input style=""width: 92px"" type=""text"" value=""{0}"" /><input style=""width: 64px"" type=""hidden"" value=""{1}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{2}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{3}"" /></td>
                    </tr>", dt.Rows[i]["ItemName"], dt.Rows[i]["ID"], dt.Rows[i]["ItemNeed"], dt.Rows[i]["comment"]));
                    }
                    ltrPlanItem.Text = Convert.ToString(sbu);
                    #endregion

                    #region //获取维修设备备件列表
                    dt = ds.Tables[2];
                    rptPart.DataSource = dt;
                    rptPart.DataBind();
                    #endregion
                }
            }
        }

        #endregion

        #region 获得产品品牌

        [Ajax.AjaxMethod]
        public string getBrandHtml(int iTypeId)
        {
            BuyDevice buy = null;
            DataTable dt = null;
            try
            {
                buy = new BuyDevice();
                dt = buy.getBrandByDeviceTypeId(iTypeId);
            }
            catch (Exception e)
            {
                return Convert.ToString(e);
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            StringBuilder sbu = new StringBuilder();
            sbu.Append(@"<select style=""width: 188px""><option selected=""selected"" value=""0"">请选择品牌</option>");
            int iRowCount = dt.Rows.Count;
            for (int i = 0; i < iRowCount; i++)
            {
                sbu.Append(string.Format(@"<option value=""{0}"">{1}</option>"
                    , dt.Rows[i]["brandid"], dt.Rows[i]["brandname"]));
            }
            sbu.Append(@"</select>");
            return Convert.ToString(sbu);
        }

        #endregion

        int m_Id
        {
            get
            {
                return Convert.ToInt32(Request["id"]);
            }
        }

        #region 保存信息

        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
        public bool saveData(int id,string dcode, int idevid,string createdate, string BeginDate, string EndDate,string Need,string[] Items, string[] parts)
        {
            BuyDevice buy = null;
            DeviceInfo deviceinfo = new DeviceInfo();
            bool bResult = false;
            try
            {
                buy = new BuyDevice();
                dcode = dcode.Replace("'", "''");
                //sReason = sReason.Replace("'", "''");
                Need = Need.Replace("'", "''");
                for (int i = 0; i < Items.Length; i++)
                {
                    Items[i] = Items[i].Replace("'", "''");
                }
                for (int i = 0; i < parts.Length; i++)
                {
                    parts[i] = parts[i].Replace("'", "''");
                }
                bResult = deviceinfo.setRepairAppInfo(id, dcode, idevid, createdate, BeginDate, EndDate, Need, Items, parts);
            }
            catch(Exception e)
            {
                Response.Write("<script languagwe='javascript'>alert('"+e.Message+"');</script>");
                bResult = false;
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            return bResult;
        }

        #endregion

        #region 获得备件信息

        [Ajax.AjaxMethod]
        public string getPartInfo11(int iTypeId)
        {
            //BuyDevice buy = null;
            DeviceInfo deviceinfo = new DeviceInfo();
            DataTable dt = null;
            try
            {
                //buy = new BuyDevice();
                dt = deviceinfo.getDevPartInfoById(iTypeId).Tables[0];
            }
            catch (Exception e)
            {
                return Convert.ToString(e);
            }
            finally
            {
                if (deviceinfo != null)
                {
                    deviceinfo.Dispose();
                }
            }
            #region //获取维修设备备件列表
            int iRowCount = dt.Rows.Count;
            StringBuilder sbu = new StringBuilder();
            for (int i = 0; i < iRowCount; i++)
            {
                sbu.Append(string.Format(@"<tr>
                        <td align=""left"" width=""120"">
                            <a href=""javascript:"" onclick=""insertdevice(this.parentNode.parentNode)"">+</a> <a href=""javascript:"" onclick=""removeItem(this.parentNode.parentNode)"">-</a><input style=""width: 92px"" type=""text"" value=""{0}"" /><input style=""width: 64px"" type=""hidden"" value=""{1}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{2}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{3}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{4}"" /></td>
                    </tr>", dt.Rows[i]["PartCode"], dt.Rows[i]["PartID"], dt.Rows[i]["PartName"], dt.Rows[i]["Xinghaogg"], dt.Rows[i]["iNeedNum"]));
            }
            //ltrPlanPart.Text = Convert.ToString(sbu);
            #endregion
            return Convert.ToString(sbu);
        }

        [Ajax.AjaxMethod]
        public DataTable getPartInfo(string sIds)
        {
            DeviceInfo deviceinfo = new DeviceInfo();
            DataTable dt = null;
            try
            {
                dt = deviceinfo.getResultDevPartInfo(sIds);
            }
            catch
            {
                dt = null;
            }
            finally
            {
                if (deviceinfo != null)
                {
                    deviceinfo.Dispose();
                }
            }
            return dt;
        }

        #endregion

        #region 审核申请

        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
        public bool audiApp(int iAppId, int iState, string sFile, string sMemo)
        {
            BuyDevice buy = null;
            bool bResult = false;
            try
            {
                buy = new BuyDevice();
                sFile = sFile.Replace("'", "''");
                sMemo = sMemo.Replace("'", "''");
                bResult = buy.audiAppInfoById(iAppId, iUserId, sFile, sMemo, iState);
            }
            catch
            {
                bResult = false;
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            return bResult;
        }

        #endregion
}
}